package com.myt.matp.gui;

import com.myt.matp.core.Event;

public abstract class UIScroll extends UIWidget {

    private UIScrollBar m_VSB = null;

    private int m_Content = 0;
    private int m_Frame = 0;
    private int m_Redisplay = 0;

    private boolean m_VerticalFrame = true;
    private boolean m_VerticalRedisplay = true;

    protected UIScroll(int id, GUI gui) {
	super(id, gui);
    }

    protected final int getContent() {
	return m_Content;
    }

    protected final int getFrame() {
	return m_Frame;
    }

    protected final int getRedisplay() {
	return m_Redisplay;
    }

    protected final void resetVerticalFrame() {
	m_VerticalFrame = true;
	resetVerticalRedisplay();
    }

    protected final void resetVerticalRedisplay() {
	m_VerticalRedisplay = true;
    }

    public void setVerticalScrollBar(UIScrollBar sb) {
	m_VSB = sb;
    }

    public final UIScrollBar getVerticalScrollBar() {
	return m_VSB;
    }

    public final boolean hasVerticalScroll() {
	return (m_VSB != null);
    }

    protected int getViewWidth() {
	int w;
	w = super.getViewWidth();
	if (m_VSB != null){
	    w -= m_VSB.getMinWidth();
	}
	return w;
    }

    protected final void paintUIWidget(MGraphics g) {

 	//#ifdef DEBUG_UISCROLL
	//# System.out.println(getClass().getName() + ".paintUIWidget() {");
	//#endif

	if (m_VerticalFrame) {
	    m_Frame = computeFrame();
	    m_VerticalFrame = false;
	}

	if (m_VerticalRedisplay) {
	    m_Redisplay = computeRedisplay(m_Frame);
	    m_Content = computeContent();	    
	    m_VerticalRedisplay = false;
	}

	// paint vertical scrollbar
	paintVerticalScrollBar(g);

	// paint scrolled content
	paintUIScroll(g);

 	//#ifdef DEBUG_UISCROLL
	//# System.out.println("}");
	//#endif
    }

    private void paintVerticalScrollBar(MGraphics g) {

        //#ifdef DEBUG_UISCROLL
        //# System.out.println(getClass().getName() + ".paintScrollBar() {");
        //#endif

	if (m_VSB != null) {
	    m_VSB.setArea(getViewWidth(), getViewHeight());
	    // Set left
	    m_VSB.setPosX(getViewX() + getViewWidth());
	    m_VSB.setPosY(getViewY());
	    m_VSB.set(getContent(), getRedisplay(), getFrame());
	    m_VSB.paint(g);
	}

        //#ifdef DEBUG_UISCROLL
        //# System.out.println("}");
        //#endif
    }

    protected abstract int computeContent();

    protected abstract int computeFrame();

    protected abstract int computeRedisplay(int frame);

    protected abstract void paintUIScroll(MGraphics g);
}